<?php

namespace app\controllers\group;

use app\common\base\CommonController;

class GroupController extends CommonController
{
    //添加群组
    public function actionCreateGroup()
    {
        $this->rules = [
            [['groupName'], 'required'],
            [['groupName'], 'string'],
        ];
        $inputs = $this->validate();

        if(!$this->userInfo->isAdmin) {
            return parent::error('ERROR_PERMISSION_DENY', '没有操作权限');
        }

        $info = parent::callModuleService('group', 'GroupService', 'createGroup', $this->userInfo->id, $inputs['groupName']);

        return [
            'success' => $info['success'],
        ];
    }

    //删除群组
    public function actionDeleteGroup()
    {
        $this->rules = [
            [['groupName'], 'required'],
            [['groupName'], 'string'],
        ];
        $inputs = $this->validate();

        if(!$this->userInfo->isAdmin) {
            return parent::error('ERROR_PERMISSION_DENY', '没有操作权限');
        }

        $info = parent::callModuleService('group', 'GroupService', 'deleteGroup', $this->userInfo->id, $inputs['groupName']);

        return [
            'success' => $info['success'],
        ];
    }

    /**
     * 展示所有群组
     */
    public function actionListGroups()
    {
        if(!$this->userInfo->isAdmin) {
            return parent::error('ERROR_PERMISSION_DENY', '没有操作权限');
        }

        $info = parent::callModuleService('group', 'GroupService', 'listGroup', $this->userInfo->id);

        return [
            'success' => $info['success'],
            'data' => $info['data']
        ];
    }
}
